package com.example.springvue.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.springvue.common.annotation.LogAnnotation;
import com.example.springvue.common.annotation.PassToken;
import com.example.springvue.common.handler.AdminUserHeaderHodler;
import com.example.springvue.common.handler.NotExistException;
import com.example.springvue.common.returnthrow.Result;
import com.example.springvue.model.AdminUser;
import com.example.springvue.model.AuthGroup;
import com.example.springvue.service.AdminUserService;
import com.example.springvue.service.AuthGroupService;
import com.example.springvue.service.AuthRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class AuthGroupController {


    @Autowired
    AdminUserService adminUserService;
    @Autowired
    AuthRuleService authRuleService;
    @Autowired
    AuthGroupService authGroupService;

    /**
     * 角色列表
     *
     * @return
     */
    @LogAnnotation
    @ResponseBody
    @PassToken
    @PostMapping(value = "/admin/authGroup/index")
    public Result<Map< String, Object >> getAuthGroupIndex(@RequestBody Map<String, Object> Body) throws NotExistException {
        try{
            int pageSize = (int) Body.get("pageSize");
            int page = (int) Body.get("page");
            Map<String, Object>  authRule = authGroupService.getPageSelect(pageSize,page);
            return Result.success("请求成功",authRule);
        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }


    /**
     * 保存
     *
     * @return
     */
    @LogAnnotation
    @ResponseBody
    @PassToken
    @PostMapping(value = "/admin/authGroup/save")
    public Result<Map< String, Object >> AuthGroupSave(@RequestBody Map<String, Object> Body) throws NotExistException {
        try{
            int id = (int) Body.get("id");
            System.out.println(Body);
            if (id == 0){
                boolean add= authGroupService.authGroupAdd(Body);
                if (add){
                    return Result.success("添加成功",null);
                }else {
                    return Result.failure("添加失败");
                }
            }else {
                boolean edit= authGroupService.authGroupEdit(Body);
                if (edit){
                    return Result.success("修改成功",null);
                }else {
                    return Result.failure("修改失败");
                }
            }
        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }


    /**
     *
     * 删除
     *
     * @return
     */
    @LogAnnotation
    @ResponseBody
    @PostMapping(value = "/admin/authGroup/delete")
    public Result<Boolean>authGroupDelete(@RequestBody Map<String, Object> Body) throws NotExistException {
        try {
            String id = (String) Body.get("id");
            List<String>  ids = Arrays.asList(id.split(","));
            boolean del = authGroupService.authGroupDelete(ids);
            if (del) {
                return Result.success("删除成功", null);
            } else {
                System.out.println(del);
                return Result.failure("删除失败");
            }

        } catch (NotExistException e) {
            throw new NotExistException(e.getMessage());
        }
    }


    /**
     * 角色下拉列表
     *
     * @return
     */
    @LogAnnotation
    @ResponseBody
    @PassToken
    @PostMapping(value = "/admin/authGroup/list")
    public Result<List<Map<String, Object>>> getList(@RequestBody Map<String, Object> Body) throws NotExistException {
        try{
           List<Map<String, Object> >  authRule = authGroupService.getList();
            return Result.success("请求成功",authRule);
        }
        catch (NotExistException e)
        {
            throw new NotExistException(e.getMessage());
        }
    }


}
